let VshaderSource =
    'attribute vec4 a_Position;\n' +
    'uniform float u_CosB, u_SinB;\n' +
    'void main(){\n' +
    '   gl_Position.x = a_Position.x * u_CosB - a_Position.y * u_SinB;\n' +
    '   gl_Position.y = a_Position.x * u_SinB + a_Position.y * u_CosB;\n' +
    '   gl_Position.z = a_Position.z;\n' +
    '   gl_Position.w = 1.0;\n' +
    '}\n'

let FshaderSource =
    'void main() {\n' +
    '  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +
    '}\n';

let ANGLE = 10.0

function main(ANGLE = 10) {
    const canvas = document.getElementById('webgl')
    const gl = getWebGLContext(canvas)
    if (!initShaders(gl, VshaderSource, FshaderSource)) {
        console.log('init shader error');
    }

    let n = initVertexBuffers(gl)

    let radian = Math.PI * ANGLE / 180.0 //转化为弧度制
    let cosB = Math.cos(radian)
    let sinB = Math.sin(radian)

    let u_CosB = gl.getUniformLocation(gl.program, 'u_CosB')
    let u_SinB = gl.getUniformLocation(gl.program, 'u_SinB')

    gl.uniform1f(u_CosB, cosB)
    gl.uniform1f(u_SinB, sinB)

    gl.clearColor(0, 0, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT);

    gl.drawArrays(gl.TRIANGLES, 0, n);
}

function initVertexBuffers(gl) {
    let vertices = new Float32Array([
        0.0, 0.5,
        -0.5, -0.5,
        0.5, -0.5
    ])
    let n = 3

    let vertexBuffer = gl.createBuffer()
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer)
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)

    let a_Position = gl.getAttribLocation(gl.program, 'a_Position')
    gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0)
    gl.enableVertexAttribArray(a_Position)
    return n
}